package sqldbManagement.dblibrary.cj.service;

import sqldbManagement.dblibrary.cj.anno.SpaceLabel;
import sqldbManagement.dblibrary.cj.model.Code;
import sqldbManagement.dblibrary.cj.model.JDBCModel;
import sqldbManagement.dblibrary.cj.utils.JDBCUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JDBCService {

    public static JDBCModel getModel(){
        return new JDBCModel();
    }



    /**
     * 关闭连接
     * @param conn
     * @param ps
     * @param rs
     */
    public static void close(Connection conn, Statement ps, ResultSet rs){
        JDBCUtil.close(conn,ps,rs);
    }
    public static void close(Connection conn,Statement ps){
        close(conn, ps, null);
    }

    public static <T> T queryOne(String sql, JDBCModel model, Class<T> clazz){
        List<T> query = query(sql, model, clazz);
        if(query == null || query.size() == 0)
            return null;
        return query.get(0);
    }

    /**
     * 执行sql语句，根据类对象获取对象
     * @param sql
     * @param model
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> query(String sql, JDBCModel model, Class<T> clazz){
       try {
           Connection conn = JDBCUtil.getConnection();
           PreparedStatement state = createStatement(conn, sql, model);
           ResultSet rs = state.executeQuery();

           List<T> tList = createObject(clazz, rs);

           close(conn,state,rs);

           return tList;
       } catch (Exception e){
           e.printStackTrace();
           return null;
       }
    }

    /**
     * 添加
     * @param sql
     * @param model
     * @return
     */
    public static int insert(String sql, JDBCModel model){
        try {
            Connection conn = JDBCUtil.getConnection();
            PreparedStatement state = createStatement(conn, sql, model);

            int i = state.executeUpdate();

            close(conn,state);
            return i;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 修改
     * @param sql
     * @param model
     * @return
     */
    public static int update(String sql, JDBCModel model){
        return insert(sql, model);
    }

    /**
     * 删除
     * @param sql
     * @param model
     * @return
     */
    public static int delete(String sql, JDBCModel model){
        return insert(sql, model);
    }

    /**
     * 解析结果集对象，封装为类对象
     * @param clazz
     * @param rs
     * @param <T>
     * @return
     */
    private static <T> List<T> createObject(Class<T> clazz,ResultSet rs){
        List<T> list = new ArrayList<>();
        try {
            while (rs.next()){
                T t = clazz.getDeclaredConstructor().newInstance();
                Field[] fields = clazz.getDeclaredFields();

                for (Field field : fields) {
                    if(field.getAnnotation(SpaceLabel.class) != null)
                        continue;

                    field.setAccessible(true);
                    field.set(t,rs.getObject(field.getName()));
                    field.setAccessible(false);
                }
                list.add(t);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解析 预处理语句
     * @param conn
     * @param sql
     * @param model
     * @return
     */
    private static PreparedStatement createStatement(Connection conn,String sql, JDBCModel model){
        try{
            PreparedStatement state = conn.prepareStatement(sql);
            if(model == null) return state;
            int i = 0;
            for (Map.Entry<Integer, JDBCModel.Nodes> entry : model.entrySet()) {
                Method m = PreparedStatement.class.getMethod(entry.getValue().getCodes().getCode(),int.class,entry.getValue().getCodes().getAClass());
                m.invoke(state,++i,entry.getValue().getObj());
            }
            return state;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


}
